<template>
  <div class="app-container">
      <el-form ref="formRef" :model="formData" label-width="120px" label-position="right" v-if="formData" :rules="rules">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="车牌号" prop="carNo">
              <el-input v-model="formData.carNo" placeholder="请输入车牌号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="进出方向" required>
              <el-select v-model="formData.direction" placeholder="请选择">
                <el-option
                    v-for="(item,index) in directionOptions"
                    :key="index"
                    :label="item.label"
                    :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出入口" prop="gateName">
              <el-input v-model="formData.gateName" placeholder="出入口名称" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="text-right" style="text-align: center">
        <el-button type="danger" @click="dialogVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="submit">
          确定
        </el-button>
      </div>
  </div>
</template>

<script>
  import date from 'date-and-time';
  import {
    testVehicleInOut
  } from '@/api/test'
  // import { datetimeFormat } from '@/utils/datetime-util'
  import {isPlateNo} from '@/utils/validate'

  export default {
    name: 'VehicleTest',
    data() {
      var checkPlate = (rule, value, callback) => {
        if (!value) {
          callback(new Error("请输入车牌号"));
        } else if(!isPlateNo(value)) {
          return callback(new Error("请输入正确的车牌号"));
        } else {
          callback();
        }
      };
      var checkEmpty = (rule, value, callback) => {
        console.log(rule, value)
        if (!value) {
          callback(new Error("必填项不能为空！"));
        } else {
          callback();
        }
      };
      return {
        directionOptions: [
          {
            value: 0,
            label: '进场'
          },
          {
            value: 1,
            label: '出场'
          }
        ],
        formData: {
          carNo: '',
          // time: datetimeFormat('YYYY-mm-dd HH:MM:SS', new Date()),
          direction: 0,
          gateName: ''
        },
        rules: {
          carNo: [{ required: true, validator: checkPlate, trigger: 'blur' }],
          // time: [{ required: true, messages: '时间必填', trigger: 'blur' }],
          gateName: [{ required: true, validator: checkEmpty, trigger: 'blur' }],
        },
        hikangForm: {
          method: "OnEventNotify",
          params: {
            ability: "event_pms",
            events: [{
              data: {
                alarmCar: 0,
                carAttributeName: "临时车",
                cardNo: "",
                eventCmd: 3,
                eventIndex: "A649E203-16C2-4E97-9405-E90D5D7E7354",
                gateIndex: "d32807d7bade4871a937eed7e4989cc6",
                gateName: "东南门出入口",
                inResult: {
                  rlsResult: {
                    releaseAuth: 16,
                    releaseReason: 370,
                    releaseResult: 0,
                    releaseResultEx: 0,
                    releaseWay: 10
                  }
                },
                inoutType: 0,
                mainLogo: 2,
                parkIndex: "374efc2941054b3082541fc614f7f7e8",
                parkName: "三期园区地面停车场",
                plateBelieve: 97,
                plateColor: 0,
                plateNo: "浙AF608S",
                plateType: 0,
                roadwayIndex: "e8d332c9e70448a6881a6a469a797b66",
                roadwayName: "常入口",
                roadwayType: 1,
                subLogo: 0,
                subModel: 0,
                time: "2018-05-21T10:54:56.955+08:00",
                vehicleClass: 1,
                vehicleColor: 255,
                vehicleType: 1
              },
              eventId: "A649E203-16C2-4E97-9405-E90D5D7E7354",
              eventType: 771760131,
              happenTime: "2018-05-21T10:54:56.955+08:00",
              srcIndex: "e8d332c9e70448a6881a6a469a797b66",
              srcParentIdex: "d32807d7bade4871a937eed7e4989cc6",
              srcType: "roadway",
              status: 0,
              timeout: 30
            }],
            sendTime: "2018-05-21T10:54:56.955+08:00"
          }
        }
      }
    },
    mounted() {
    },
    methods: {
      uuid() {
        var s = [];
        var hexDigits = "0123456789abcdef";
        for (var i = 0; i < 36; i++) {
          s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
        }
        s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
        s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
        s[8] = s[13] = s[18] = s[23] = "-";

        var uuid = s.join("");
        return uuid;
      },
      openDetailDialog(roleId) {
        roleDetail(roleId).then(response => {
          if (response.code === 200 ){
            this.detailData = response.data
            this.detailDialogVisible = true
          } else {
            this.$message.error('获取角色详情失败：' + response.msg)
          }
        })
      },
      submit() {
        this.$refs.formRef.validate(valid => {
          if (valid) {
            this.hikangForm.params.events[0].eventIndex = this.uuid()
            this.hikangForm.params.events[0].data.plateNo = this.formData.carNo
            this.hikangForm.params.events[0].data.gateName = this.formData.gateName
            this.hikangForm.params.events[0].data.inoutType = this.formData.direction
            if (this.formData.direction === 0){
              this.hikangForm.params.events[0].eventType = 771760131
            } else {
              this.hikangForm.params.events[0].eventType = 771760134
            }
            var d = new Date();
            let isoTime = date.format(d, 'YYYY-MM-DDTHH:mm:ss.SSSZ')
            var newTimeStr = isoTime.slice(0,26) + ":" + isoTime.slice(26)
            this.hikangForm.params.events[0].data.time = newTimeStr
            this.hikangForm.params.events[0].happenTime = newTimeStr
            this.hikangForm.params.sendTime = newTimeStr
            // 新增
            testVehicleInOut(this.hikangForm).then(response => {
              if (response.code === 200) {
                this.$message({
                  message: '测试数据提交成功',
                  type: 'success'
                })
              } else {
                this.$message.error("提交失败：" + response.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
